unit Class_TYPE_IN_GKZF;

interface
uses
  Classes,SysUtils,Uni,UniEngine;

type
  TTYPE=class(TUniEngine)
  private
    FUNITLINK: string;
    FCORDIDEX: Integer;
    FTYPEIDEX: Integer;
    FTYPECODE: string;
    FTYPEPREV: string;
    FTYPELEVL: Integer;
    FTYPELINK: string;
    FTYPENAME: string;
    FTYPEMEMO: string;
    FTYPEORDR: Integer;
    FNAMELINK: string;
  private
    FCordType: string;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property UNITLINK: string read FUNITLINK write FUNITLINK;
    property CORDIDEX: Integer read FCORDIDEX write FCORDIDEX;
    property TYPEIDEX: Integer read FTYPEIDEX write FTYPEIDEX;
    property TYPECODE: string read FTYPECODE write FTYPECODE;
    property TYPEPREV: string read FTYPEPREV write FTYPEPREV;
    property TYPELEVL: Integer read FTYPELEVL write FTYPELEVL;
    property TYPELINK: string read FTYPELINK write FTYPELINK;
    property TYPENAME: string read FTYPENAME write FTYPENAME;
    property TYPEMEMO: string read FTYPEMEMO write FTYPEMEMO;
    property TYPEORDR: Integer read FTYPEORDR write FTYPEORDR;
    property NAMELINK: string read FNAMELINK write FNAMELINK;
    property CORDTYPE: string  read FCordType write FCordType;   
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(ATYPE:TTYPE):TTYPE;overload;        
    class procedure CopyIt(ATYPE:TTYPE;var Result:TTYPE);overload;

    class function  ExpSQL:string;      
  end;

implementation

{ TTYPE }
procedure TTYPE.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('CORD_IDEX').Value := CORDIDEX;
        ParamByName('TYPE_IDEX').Value := TYPEIDEX;
        ParamByName('TYPE_CODE').Value := TYPECODE;
        ParamByName('TYPE_PREV').Value := TYPEPREV;
        ParamByName('TYPE_LEVL').Value := TYPELEVL;
        ParamByName('TYPE_LINK').Value := TYPELINK;
        ParamByName('TYPE_NAME').Value := TYPENAME;
        ParamByName('TYPE_MEMO').Value := TYPEMEMO;
        ParamByName('TYPE_ORDR').Value := TYPEORDR;
        ParamByName('NAME_LINK').Value := NAMELINK; 
      end;
      otEdit:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('CORD_IDEX').Value := CORDIDEX;
        ParamByName('TYPE_IDEX').Value := TYPEIDEX;
        ParamByName('TYPE_CODE').Value := TYPECODE;
        ParamByName('TYPE_PREV').Value := TYPEPREV;
        ParamByName('TYPE_LEVL').Value := TYPELEVL;
        ParamByName('TYPE_LINK').Value := TYPELINK;
        ParamByName('TYPE_NAME').Value := TYPENAME;
        ParamByName('TYPE_MEMO').Value := TYPEMEMO;
        ParamByName('TYPE_ORDR').Value := TYPEORDR;
        ParamByName('NAME_LINK').Value := NAMELINK; 
      end;  
      otDelt:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('CORD_IDEX').Value := CORDIDEX;
        ParamByName('TYPE_IDEX').Value := TYPEIDEX;
 
      end;  
    end;
  end;
end;

function TTYPE.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('GKX_TYPE',['UNIT_LINK',UNITLINK,'CORD_IDEX',CORDIDEX,'TYPE_IDEX',TYPEIDEX],AUniConnection);
end;

function TTYPE.GetNextIdex: Integer;
begin

end;

function TTYPE.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin
  Result:=CheckField('TYPE_IDEX','GKX_TYPE',['UNIT_LINK',UNITLINK,'CORD_IDEX',CORDIDEX],AUniConnection);
end;

function TTYPE.GetStrDelete: string;
begin
  Result:='DELETE FROM GKX_TYPE WHERE   UNIT_LINK=:UNIT_LINK AND CORD_IDEX=:CORD_IDEX AND TYPE_IDEX=:TYPE_IDEX';
end;

function TTYPE.GetStrInsert: string;
begin
  Result:='INSERT INTO GKX_TYPE'
         +'    ( UNIT_LINK, CORD_IDEX, TYPE_IDEX, TYPE_CODE, TYPE_PREV'
         +'    , TYPE_LEVL, TYPE_LINK, TYPE_NAME, TYPE_MEMO, TYPE_ORDR'
         +'    , NAME_LINK)'
         +'    VALUES'
         +'    (:UNIT_LINK,:CORD_IDEX,:TYPE_IDEX,:TYPE_CODE,:TYPE_PREV'
         +'    ,:TYPE_LEVL,:TYPE_LINK,:TYPE_NAME,:TYPE_MEMO,:TYPE_ORDR'
         +'    ,:NAME_LINK)';
end;

function TTYPE.GetStrsIndex: string;
begin
  Result:=Format('%S-%D-%D',[UNITLINK,CORDIDEX,TYPEIDEX]);
end;

function TTYPE.GetStrUpdate: string;
begin
  Result:='UPDATE  GKX_TYPE SET'
         +'    TYPE_CODE=:TYPE_CODE,'
         +'    TYPE_PREV=:TYPE_PREV,'
         +'    TYPE_LEVL=:TYPE_LEVL,'
         +'    TYPE_LINK=:TYPE_LINK,'
         +'    TYPE_NAME=:TYPE_NAME,'
         +'    TYPE_MEMO=:TYPE_MEMO,'
         +'    TYPE_ORDR=:TYPE_ORDR,'
         +'    NAME_LINK=:NAME_LINK'
         +'    WHERE UNIT_LINK=:UNIT_LINK'
         +'    AND   CORD_IDEX=:CORD_IDEX'
         +'    AND   TYPE_IDEX=:TYPE_IDEX';
end;

constructor TTYPE.Create;
begin
  FUNITLINK:='-1';
end;

destructor TTYPE.Destroy;
begin

  inherited;
end;

class function TTYPE.ReadDS(AUniQuery: TUniQuery): TUniEngine;
begin
  Result:=TTYPE.Create;
  with TTYPE(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    CORDIDEX:=AUniQuery.FieldByName('CORD_IDEX').AsInteger;
    TYPEIDEX:=AUniQuery.FieldByName('TYPE_IDEX').AsInteger;
    TYPECODE:=AUniQuery.FieldByName('TYPE_CODE').AsString;
    TYPEPREV:=AUniQuery.FieldByName('TYPE_PREV').AsString;
    TYPELEVL:=AUniQuery.FieldByName('TYPE_LEVL').AsInteger;
    TYPELINK:=AUniQuery.FieldByName('TYPE_LINK').AsString;
    TYPENAME:=AUniQuery.FieldByName('TYPE_NAME').AsString;
    TYPEMEMO:=AUniQuery.FieldByName('TYPE_MEMO').AsString;
    TYPEORDR:=AUniQuery.FieldByName('TYPE_ORDR').AsInteger;
    NAMELINK:=AUniQuery.FieldByName('NAME_LINK').AsString;

    if AUniQuery.FindField('CORD_TYPE')<>nil then
    begin
      FCordType:=AUniQuery.FieldByName('CORD_TYPE').AsString;
    end;
  end;  
end;

class procedure TTYPE.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
begin
  if Result=nil then Exit;
  with TTYPE(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    CORDIDEX:=AUniQuery.FieldByName('CORD_IDEX').AsInteger;
    TYPEIDEX:=AUniQuery.FieldByName('TYPE_IDEX').AsInteger;
    TYPECODE:=AUniQuery.FieldByName('TYPE_CODE').AsString;
    TYPEPREV:=AUniQuery.FieldByName('TYPE_PREV').AsString;
    TYPELEVL:=AUniQuery.FieldByName('TYPE_LEVL').AsInteger;
    TYPELINK:=AUniQuery.FieldByName('TYPE_LINK').AsString;
    TYPENAME:=AUniQuery.FieldByName('TYPE_NAME').AsString;
    TYPEMEMO:=AUniQuery.FieldByName('TYPE_MEMO').AsString;
    TYPEORDR:=AUniQuery.FieldByName('TYPE_ORDR').AsInteger;
    NAMELINK:=AUniQuery.FieldByName('NAME_LINK').AsString;

    if AUniQuery.FindField('CORD_TYPE')<>nil then
    begin
      FCordType:=AUniQuery.FieldByName('CORD_TYPE').AsString;
    end;
  end;  
end;

class function  TTYPE.CopyIt(ATYPE: TTYPE): TTYPE;
begin
  Result:=TTYPE.Create;
  TTYPE.CopyIt(ATYPE,Result)
end;

class procedure TTYPE.CopyIt(ATYPE:TTYPE;var Result:TTYPE);
begin
  if Result=nil then Exit;
  Result.UNITLINK:=ATYPE.UNITLINK;
  Result.CORDIDEX:=ATYPE.CORDIDEX;
  Result.TYPEIDEX:=ATYPE.TYPEIDEX;
  Result.TYPECODE:=ATYPE.TYPECODE;
  Result.TYPEPREV:=ATYPE.TYPEPREV;
  Result.TYPELEVL:=ATYPE.TYPELEVL;
  Result.TYPELINK:=ATYPE.TYPELINK;
  Result.TYPENAME:=ATYPE.TYPENAME;
  Result.TYPEMEMO:=ATYPE.TYPEMEMO;
  Result.TYPEORDR:=ATYPE.TYPEORDR;
  Result.NAMELINK:=ATYPE.NAMELINK;

  Result.CORDTYPE:=ATYPE.CORDTYPE;
end;

class function TTYPE.ExpSQL: string;
begin
  Result:='SELECT A.UNIT_LINK'
         +'    ,A.CORD_IDEX'
         +'    ,A.TYPE_IDEX'
         +'    ,A.TYPE_CODE'
         +'    ,A.TYPE_PREV'
         +'    ,A.TYPE_LEVL'
         +'    ,A.TYPE_LINK'
         +'    ,A.TYPE_NAME'
         +'    ,A.TYPE_MEMO'
         +'    ,A.TYPE_ORDR'
         +'    ,A.NAME_LINK'
         +'    ,B.CORD_TYPE'
         +'    FROM GKX_TYPE A,GKX_CORD B'
         +'    WHERE A.CORD_IDEX=B.CORD_IDEX'
         +'    ORDER BY A.UNIT_LINK,B.CORD_TYPE,A.TYPE_IDEX';
end;

end.

